// 每个接口度需要接口数据模型定义
import {MemberEntity} from '../model/member'
// Axios -> json -> react 
// Axios -> Promise<T> -> AxiosResponse<any[]> -> {data} -> MemberEntity[]
import Axios,{AxiosResponse} from 'axios';
// ts + react /model 

const githubURL="https://api.gitup.com";  // 公共域名 可读性
const gitupMemberUrl=`${githubURL}/orgs/lemoncode/members`

/**
 * Member[]
 * 
 */

export const getMembersCollection=():Promise<MemberEntity[]>=>{
        const promise=new Promise<MemberEntity[]>((resolve,reject)=>{
            try {
                // response -> MemberEntity
                Axios.get<MemberEntity[]>(gitupMemberUrl).then(response=>{
                    resolve(mapMemberListApiToModel(response))
                })
            } catch (error) {
                reject(error)
            }
        })
        return promise;
}

const mapMemberListApiToModel=({data}:AxiosResponse<any[]>):MemberEntity[]=>
    data.map(gitupMember=>({
        id:gitupMember.id,
        login:gitupMember.login,
        avatar_url:gitupMember.avatarurl
}))